<?php
class Admin_CategoryController extends My_Controller_Admin_Choix
{
	protected $categoryModel = '';

	protected function _init()
	{
		$this->categoryModel = new Admin_Model_Category();
	}

	public function indexAction()
	{
	    $numVente = $this->_getParam('num_vente');
		$rowPerPage = $this->_getParam('rowperpage', 20);
		$currentPage = 1;
		$page = $this->_getParam('page', 1);
		if(!empty($page)) {
			$currentPage = $page;
		}

		$rows = $this->categoryModel->getCollection($numVente);
		$paginator = Zend_Paginator::factory($rows);
		$paginator->setItemCountPerPage($rowPerPage);
		$paginator->setCurrentPageNumber($currentPage);

		$this->view->page = $page;
		$this->view->paginator = $paginator;
	}

	public function editAction()
	{
	    $id = $this->_getParam('id');
	    $data = array();
	    if ($id) {
	        $data = Mage::getModel('admin/category')->getCategory($id);
	    }
	    $this->view->object = new My_Object($data);
	}

	public function addAction()
	{
		$this->_forward('edit');
	}

	public function precAction()
	{
	    $numVente = $this->_getParam('num_vente');
	    $rowPerPage = $this->_getParam('rowperpage', 20);
	    $currentPage = 1;
	    $page = $this->_getParam('page', 1);
	    if(!empty($page)) {
	        $currentPage = $page;
	    }

	    $rows = $this->categoryModel->getCollectionPrec($numVente);
	    $this->view->rows = $rows;
	}

	public function recoverAction()
	{
	    $recovers = $this->_getParam('torecover');
	    if ($recovers) {
	        $numVente = $this->_getParam('num_vente');
            Mage::getModel('admin/category')->recover($recovers, $numVente);
	    }
	    $this->_goBack();
	}

	public function saveAction()
	{
        $data = $this->_getParam('data');
        $id = $this->_getParam('id', null);
        $category = Mage::getModel('admin/category');
        if (!$id) {
            $data['f_ventes'] = $this->_getParam('num_vente');
        }
        $category->save($data, $id);

        $this->_goBack();
	}

	public function deleteAction()
	{
	    $id = $this->_getParam('id', null);
	    $category = Mage::getModel('admin/category');
	    $data['deleted'] = 1;
	    $category->save($data, $id);

	    $this->_goBack();
	}
}

